﻿using lBll;
using lDal;
using Model;
using System.Runtime.InteropServices;

namespace BLL
{
    public class juMinBll : ljuMinBll
    {
        public juMinBll(lBaseDal<JuMin> dal, lBaseDal<JuXia> dal1)
        {
            Dal = dal;
            Dal1 = dal1;
        }

        public lBaseDal<JuMin> Dal { get; }
        public lBaseDal<JuXia> Dal1 { get; }

        public Task<int> Add(JuMin juMin)
        {
            juMin.IsDeleted= true;
            return Dal.AddAsync(juMin);
        }

        public Task<int> Delete(int id)
        {
            var m = Dal.GetList().FirstOrDefault(x => x.Id == id);
            m.IsDeleted = false;
            return Dal.UpdateAsync(m);
        }

        

        public dynamic Find(int id)
        {
            return Dal.GetList().FirstOrDefault(x => x.Id == id);
        }

        public dynamic Show(string? name, int? type, int index, int size)
        {
            var m = Dal.GetList().Where(x => x.IsDeleted == true);
            var types = Dal1.GetList();
            var query = from a in m
                        join b in types
                        on a.Typeid equals b.Tid
                        select new
                        {
                            a.Id,
                            a.Name,
                            a.Typeid,
                            a.Img,
                            a.IsDeleted,
                            a.LouDong,
                            a.FangJian,
                            a.Zt,
                            b.Tid,
                            b.Tname
                        };
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(x => x.Name.Contains(name));
            }
            if (type > 0)
            {
                query = query.Where(x => x.Typeid == type);
            }
            var count=query.Count();
            var list = query.OrderBy(x => x.Id).Skip((index - 1) * size).Take(size).ToList();
            return new
            {
                count,
                list
            };
        }

        public Task<int> Update(JuMin juMin)
        {
            juMin.IsDeleted = true;
            return Dal.UpdateAsync(juMin);
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="idstring"></param>
        /// <returns></returns>
        public Task<int> Updstate(string idstring)
        {
            List<int> ids=idstring.Split(',').Select(x=>Convert.ToInt32(x)).ToList();
            var Zt=Dal.GetList().Where(x=>ids.Contains(x.Id)).ToList();
            foreach (var tr in Zt)
            {
                if (tr.Zt == 1)
                {
                    tr.Zt = 2;
                }
                else
                {
                    tr.Zt = 1;
                }
            }
            return Dal.UpdateRangeAsync(Zt);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idstring"></param>
        /// <returns></returns>
        public Task<int> Delted(string idstring)
        {
            List<int> ids = idstring.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            var Zt = Dal.GetList().Where(x => ids.Contains(x.Id)).ToList();
            foreach (var tr in Zt)
            {
                tr.IsDeleted = false;
            }
            return Dal.UpdateRangeAsync(Zt);
        }

        public dynamic Xia()
        {
            return Dal1.GetList();
        }
    }
}
